package com.aolei.mallshop.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aolei.common.utils.HttpUtils;
import com.aolei.common.utils.R;
import com.aolei.mallshop.auth.feign.MemberFeignService;
import com.aolei.common.vo.MemberEntityVo;
import com.aolei.mallshop.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller()
public class OAuth2Controller {

    @Autowired
    private MemberFeignService memberFeignService;

    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "aa";
    }

    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, RedirectAttributes redirectAttributes, HttpSession session) throws Exception {

        //用code换取accessToken
        Map<String,String> map=new HashMap<>();
        map.put("code",code);
        map.put("grant_type","authorization_code");
        map.put("client_id","7dbae2d20077b6d249748049c4f8f65aad8efc91a20fcc21cec0f3f4510be492");
        map.put("redirect_uri","http://auth.mallshop.com/oauth2.0/gitee/success");
        map.put("client_secret","40c98c80e4d4cb3258a321a0be0c48786c8ee384f7acc30ee0e9fe00c392d3ea");

        //https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), map,new HashMap<>());
        //处理
        if (response.getStatusLine().getStatusCode()==200){
            //获取到了token
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            R r = memberFeignService.oauthlogin(socialUser);
            if (r.getCode()==0){
                MemberEntityVo data = r.getData("data", new TypeReference<MemberEntityVo>(){});
                log.info("data=>{}",data);
                session.setAttribute("loginUser",data);
                return "redirect:http://mallshop.com";
            }else {
                Map<String,String> map1 = new HashMap<>();
                map1.put("msg",r.getData("msg",new TypeReference<String>(){}));
                redirectAttributes.addFlashAttribute("errors",map1);
                return "redirect:http://auth.mallshop.com/login.html";
            }

        }else{
            return "redirect:http://auth.mallshop.com/login.html";
        }

    }
}
